package com.firexxx.study.threadpool;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @description: TODO
 * @author: zhuangzhang2
 * @date: 2020-11-05 09:55
 */
@Slf4j
public class ConsumerPool extends Thread {

    @Getter
    private BlockingQueue<ITask> queue = null;

    private ThreadPoolExecutor service = null;

    public ConsumerPool(int threadNumber, int queueSize) {
        queue = new LinkedBlockingQueue<>(queueSize);

        service = new ThreadPoolExecutor(threadNumber, threadNumber,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>());

        for (int i=0; i<threadNumber; i++) {
            service.execute(new Consumer(queue));
        }

        this.start();
    }

    @Override
    public void run() {
        while (true) {
            try {
                print();

                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void print() {
        log.info("消费者队列活跃线程数:{},队列大小:{}", service.getActiveCount(), queue.size());
    }
}
